Completed
Push — master ( b89bfa...6ac893 )
by Alejandro
23s queued 10s
created

ShortUrlsRow.js ➔ render   A

Complexity

Conditions 1

Size

Total Lines 37
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 37
ccs 3
cts 3
cp 1
rs 9.064
c 0
b 0
f 0
cc 1
crap 1
1
import { isEmpty } from 'ramda';
2
import React from 'react';
3
import Moment from 'react-moment';
4
import PropTypes from 'prop-types';
5
import { ExternalLink } from 'react-external-link';
6
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
7
import { faCopy as copyIcon } from '@fortawesome/free-regular-svg-icons';
8
import { CopyToClipboard } from 'react-copy-to-clipboard';
9
import { shortUrlsListParamsType } from '../reducers/shortUrlsListParams';
10
import { serverType } from '../../servers/prop-types';
11
import { shortUrlType } from '../reducers/shortUrlsList';
12
import Tag from '../../tags/helpers/Tag';
13
import ShortUrlVisitsCount from './ShortUrlVisitsCount';
14
import './ShortUrlsRow.scss';
15
16 1
const propTypes = {
17
  refreshList: PropTypes.func,
18
  shortUrlsListParams: shortUrlsListParamsType,
19
  selectedServer: serverType,
20
  shortUrl: shortUrlType,
21
};
22
23 1
const ShortUrlsRow = (
24
  ShortUrlsRowMenu,
25
  colorGenerator,
26
  useStateFlagTimeout
27
) => {
28 7
  const ShortUrlsRowComp = ({ shortUrl, selectedServer, refreshList, shortUrlsListParams }) => {
29 8
    const [ copiedToClipboard, setCopiedToClipboard ] = useStateFlagTimeout(false);
30 8
    const renderTags = (tags) => {
31 8
      if (isEmpty(tags)) {
32 1
        return <i className="indivisible"><small>No tags</small></i>;
33
      }
34
35 7
      const selectedTags = shortUrlsListParams.tags || [];
36
37 7
      return tags.map((tag) => (
38 14
        <Tag
39
          colorGenerator={colorGenerator}
40
          key={tag}
41
          text={tag}
42
          onClick={() => refreshList({ tags: [ ...selectedTags, tag ] })}
43
        />
44
      ));
45
    };
46
47 8
    return (
48
      <tr className="short-urls-row">
49
        <td className="indivisible short-urls-row__cell" data-th="Created at: ">
50
          <Moment format="YYYY-MM-DD HH:mm">{shortUrl.dateCreated}</Moment>
51
        </td>
52
        <td className="short-urls-row__cell" data-th="Short URL: ">
53
          <span className="indivisible short-urls-row__cell--relative">
54
            <ExternalLink href={shortUrl.shortUrl} />
55
            <CopyToClipboard text={shortUrl.shortUrl} onCopy={setCopiedToClipboard}>
56
              <FontAwesomeIcon icon={copyIcon} className="ml-2 short-urls-row__copy-btn" />
57
            </CopyToClipboard>
58
            <span className="badge badge-warning short-urls-row__copy-hint" hidden={!copiedToClipboard}>
59
              Copied short URL!
60
            </span>
61
          </span>
62
        </td>
63
        <td className="short-urls-row__cell short-urls-row__cell--break" data-th="Long URL: ">
64
          <ExternalLink href={shortUrl.longUrl} />
65
        </td>
66
        <td className="short-urls-row__cell" data-th="Tags: ">{renderTags(shortUrl.tags)}</td>
67
        <td className="short-urls-row__cell text-md-right" data-th="Visits: ">
68
          <ShortUrlVisitsCount
69
            visitsCount={shortUrl.visitsCount}
70
            shortUrl={shortUrl}
71
            selectedServer={selectedServer}
72
          />
73
        </td>
74
        <td className="short-urls-row__cell">
75
          <ShortUrlsRowMenu selectedServer={selectedServer} shortUrl={shortUrl} />
76
        </td>
77
      </tr>
78
    );
79
  };
80
81 7
  ShortUrlsRowComp.propTypes = propTypes;
82
83 7
  return ShortUrlsRowComp;
84
};
85
86
export default ShortUrlsRow;
87